Trò chơi xếp gạch đơn giản
7.083 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3 using System;
4 using UnityEngine.UI;
5
6 public class GameManager : MonoBehaviour
7 {
8
9 Text statusText;
10
11 // Use this for initialization
12 void Start()
13 {
14 blocks = GameObject.FindGameObjectsWithTag("Block");
15 Ball = GameObject.Find("Ball").GetComponent<BallScript>();
16 statusText = GameObject.Find("Status").GetComponent<Text>();
17 }
18
19
20 private bool InputTaken()
21 {
22 return Input.touchCount > 0 || Input.GetMouseButtonUp(0);
23 }
24
25
26 // Update is called once per frame
27 void Update()
28 {
29 switch (CurrentGameState)
30 {
31 case GameState.Start:
32 if (InputTaken())
33 {
34 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
35 CurrentGameState = GameState.Playing;
36 Ball.StartBall();
37 }
38 break;
39 case GameState.Playing:
40 break;
41 case GameState.Won:
42 if (InputTaken())
43 {
44 Restart();
45 Ball.StartBall();
46 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
47 CurrentGameState = GameState.Playing;
48 }
49 break;
50 case GameState.LostALife:
51 if (InputTaken())
52 {
53 Ball.StartBall();
54 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
55 CurrentGameState = GameState.Playing;
56 }
57 break;
58 case GameState.LostAllLives:
59 if (InputTaken())
60 {
61 Restart();
62 Ball.StartBall();
63 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
64 CurrentGameState = GameState.Playing;
65 }
66 break;
67 default:
68 break;
69 }
70 }
71
72 private void Restart()
73 {
74 foreach (var item in blocks)
75 {
76 item.SetActive(true);
77 item.GetComponent<BlockScript>().InitializeColor();
78 }
79 Lives = 3;
80 Score = 0;
81 }
82
83
84 public void DecreaseLives()
85 {
86 if (Lives > 0)
87 Lives--;
88
89 if(Lives == 0)
90 {
91 statusText.text = "Lost all lives. Tap to play again";
92 CurrentGameState = GameState.LostAllLives;
93 }
94 else
95 {
96 statusText.text = "Lost a life. Tap to continue";
97 CurrentGameState = GameState.LostALife;
98 }
99 Ball.StopBall();
100 }
101
102 public static int Lives = 3;
103 public static int Score = 0;
104 public static int BlocksAlive = 20;
105 public static GameState CurrentGameState = GameState.Start;
106
107 public static BallScript Ball;
108 private GameObject[] blocks;
109
110 public enum GameState
111 {
112 Start,
113 Playing,
114 Won,
115 LostALife,
116 LostAllLives
117 }
118 }
2 using System.Collections;
3 using System;
4 using UnityEngine.UI;
5
6 public class GameManager : MonoBehaviour
7 {
8
9 Text statusText;
10
11 // Use this for initialization
12 void Start()
13 {
14 blocks = GameObject.FindGameObjectsWithTag("Block");
15 Ball = GameObject.Find("Ball").GetComponent<BallScript>();
16 statusText = GameObject.Find("Status").GetComponent<Text>();
17 }
18
19
20 private bool InputTaken()
21 {
22 return Input.touchCount > 0 || Input.GetMouseButtonUp(0);
23 }
24
25
26 // Update is called once per frame
27 void Update()
28 {
29 switch (CurrentGameState)
30 {
31 case GameState.Start:
32 if (InputTaken())
33 {
34 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
35 CurrentGameState = GameState.Playing;
36 Ball.StartBall();
37 }
38 break;
39 case GameState.Playing:
40 break;
41 case GameState.Won:
42 if (InputTaken())
43 {
44 Restart();
45 Ball.StartBall();
46 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
47 CurrentGameState = GameState.Playing;
48 }
49 break;
50 case GameState.LostALife:
51 if (InputTaken())
52 {
53 Ball.StartBall();
54 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
55 CurrentGameState = GameState.Playing;
56 }
57 break;
58 case GameState.LostAllLives:
59 if (InputTaken())
60 {
61 Restart();
62 Ball.StartBall();
63 statusText.text = string.Format("Lives: {0} Score: {1}", Lives, Score);
64 CurrentGameState = GameState.Playing;
65 }
66 break;
67 default:
68 break;
69 }
70 }
71
72 private void Restart()
73 {
74 foreach (var item in blocks)
75 {
76 item.SetActive(true);
77 item.GetComponent<BlockScript>().InitializeColor();
78 }
79 Lives = 3;
80 Score = 0;
81 }
82
83
84 public void DecreaseLives()
85 {
86 if (Lives > 0)
87 Lives--;
88
89 if(Lives == 0)
90 {
91 statusText.text = "Lost all lives. Tap to play again";
92 CurrentGameState = GameState.LostAllLives;
93 }
94 else
95 {
96 statusText.text = "Lost a life. Tap to continue";
97 CurrentGameState = GameState.LostALife;
98 }
99 Ball.StopBall();
100 }
101
102 public static int Lives = 3;
103 public static int Score = 0;
104 public static int BlocksAlive = 20;
105 public static GameState CurrentGameState = GameState.Start;
106
107 public static BallScript Ball;
108 private GameObject[] blocks;
109
110 public enum GameState
111 {
112 Start,
113 Playing,
114 Won,
115 LostALife,
116 LostAllLives
117 }
118 }
Use this for initialization
Update is called once per frame